<?php


namespace addon\hotel\model;

use app\model\BaseModel;
use app\model\hotel\Stat;


class HotelBrowse extends BaseModel
{
    public function addBrowse($data)
    {
        $res = model('hotel_browse')->getInfo([ [ 'member_id', '=', $data['member_id'] ], [ 'hotel_id', '=', $data['hotel_id'] ] ], 'hotel_id,id');
        $data['browse_time'] = time();
        if (!empty($res)) {
            $collect_id = model('hotel_browse')->update($data, [ [ 'id', '=', $res['id'] ] ]);
        } else {
            $collect_id = model('hotel_browse')->add($data);
        }

        //添加浏览统计
        $stat = new Stat();
        $stat->addHotelStat(['visit_count' => 1, 'hotel_id' => $data['hotel_id']]);

        return $this->success($collect_id);
    }

    /**
     * 删除浏览记录
     * @param int $id
     * @param int $member_id
     */
    public function deleteBrowse($id, $member_id)
    {
        $res = model('hotel_browse')->delete([ [ 'member_id', '=', $member_id ], [ 'id', '=', $id ] ]);

        return $this->success($res);
    }

    public function bathDeleteBrowse($id, $member_id)
    {
        $res = model('hotel_browse')->delete([ [ 'member_id', '=', $member_id ], [ 'id', 'in', $id ] ]);

        return $this->success($res);
    }

    /**
     * 获取浏览记录分页列表
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getBrowsePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'browse_time desc', $field = '*', $alias = 'a', $join = [])
    {
        $list = model('hotel_browse')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);

        return $this->success($list);
    }
}